package com.waimai.demo.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.waimai.demo.Service.AddressBookService;
import com.waimai.demo.back.BaseContext;
import com.waimai.demo.back.R;
import com.waimai.demo.domain.AddressBook;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {
    @Autowired
    AddressBookService addressBookService;

    @GetMapping("/list")
    public R<List> list() {
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<AddressBook> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(AddressBook::getUserId, userId);
        List<AddressBook> list = addressBookService.list(lambdaQueryWrapper);
        return R.success(list);
    }

    @PostMapping
    public R<String> add(@RequestBody AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setUpdateTime(LocalDateTime.now());
        addressBook.setCreateTime(LocalDateTime.now());
        addressBook.setUpdateUser(BaseContext.getCurrentId());
        addressBook.setCreateUser(BaseContext.getCurrentId());
        addressBookService.save(addressBook);
        return R.success("新增地址成功");
    }

    @GetMapping("/{id}")
    public R<AddressBook> huixian(@PathVariable Long id) {
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<AddressBook> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(AddressBook::getUserId, userId);
        lambdaQueryWrapper.eq(AddressBook::getId, id);
        AddressBook one = addressBookService.getOne(lambdaQueryWrapper);
        return R.success(one);
    }

    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook) {
        LambdaQueryWrapper<AddressBook> lambdaQueryWrapper = new LambdaQueryWrapper();

        addressBookService.update();
        return R.success("修改地址成功");
    }

    @PutMapping("/default")
    public R<AddressBook> Default(@RequestBody AddressBook addressBook) {
        LambdaUpdateWrapper<AddressBook> lambdaUpdateWrapper = new LambdaUpdateWrapper();
        lambdaUpdateWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        lambdaUpdateWrapper.set(AddressBook::getIsDefault, 0);
        addressBookService.update(lambdaUpdateWrapper);
        addressBook.setIsDefault(1);
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setUpdateTime(LocalDateTime.now());
        addressBook.setCreateTime(LocalDateTime.now());
        addressBook.setUpdateUser(BaseContext.getCurrentId());
        addressBook.setCreateUser(BaseContext.getCurrentId());
        addressBookService.updateById(addressBook);
        return R.success(addressBook);
    }

    @GetMapping("default")
    public R<AddressBook> getDefault() {
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        queryWrapper.eq(AddressBook::getIsDefault, 1);

        //SQL:select * from address_book where user_id = ? and is_default = 1
        AddressBook addressBook = addressBookService.getOne(queryWrapper);

        if (null == addressBook) {
            return R.error("没有找到该对象");
        } else {
            return R.success(addressBook);
        }
    }
}
